using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;

public class TouchEvent : MonoBehaviour
{
	public static TouchEvent instance;

	private float minSwipeDistance;

	public int getEnemyNumberOnTouched;

	public LayerMask allowLayers;

	public int currentEnemyNumber;

	public Vector2 touchDownPos;

	public Vector2 releasePos;

	[HideInInspector]
	public Coroutine CO_PlayerAI;

	private Coroutine CO_TouchEvent;

	private void Awake()
	{
		instance = this;
		minSwipeDistance = (float)(Screen.width * Screen.height) * 1E-06f + 2f;
	}

	public void Init()
	{
		StopAllCoroutines();
		touchDownPos = Vector2.zero;
		releasePos = Vector2.zero;
		currentEnemyNumber = -1;
		getEnemyNumberOnTouched = -1;
		if (CO_PlayerAI != null)
		{
			StopCoroutine(CO_PlayerAI);
		}
		CO_PlayerAI = StartCoroutine(PlayerAI());
	}

	public void TouchEventStart()
	{
		if (CO_TouchEvent != null)
		{
			StopCoroutine(CO_TouchEvent);
			CO_TouchEvent = null;
		}
		if (CO_PlayerAI != null)
		{
			StopCoroutine(CO_PlayerAI);
			CO_PlayerAI = null;
		}
		CO_TouchEvent = StartCoroutine(IE_TouchEvent());
	}

	private IEnumerator IE_TouchEvent()
	{
		while (Player.instance.hp > 0f)
		{
			yield return null;
			if (Input.GetMouseButtonDown(0))
			{
				touchDownPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
				if (touchDownPos.y < 2f)
				{
					touchDownPos = new Vector2(touchDownPos.x, 2f);
				}
			}
			else
			{
				if (!Input.GetMouseButtonUp(0) || EventSystem.current.IsPointerOverGameObject())
				{
					continue;
				}
				releasePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
				if (releasePos.y < 2f)
				{
					releasePos = new Vector2(releasePos.x, 2f);
				}
				if (Vector2.Distance(touchDownPos, releasePos) > minSwipeDistance)
				{
					Player.instance.LookAtTheTarget(releasePos, new Vector2(-99999f, -99999f));
					Player.instance.kindOfMotion = 6;
					continue;
				}
				getEnemyNumberOnTouched = Calc.FindEnemiesForMeleeAttack(releasePos);
				if (getEnemyNumberOnTouched == -1)
				{
					Player.instance.kindOfMotion = 1;
					continue;
				}
				currentEnemyNumber = getEnemyNumberOnTouched;
				if (!Player.instance.attackingNow)
				{
					if (Player.instance.GetKindOfWeaponNumber() == 6)
					{
						Player.instance.kindOfMotion = 5;
					}
					else
					{
						Player.instance.kindOfMotion = 2;
					}
				}
			}
		}
	}

	private IEnumerator PlayerAI()
	{
		yield return new WaitForSeconds(1f);
		while (Player.instance.hp > 0f)
		{
			yield return new WaitForSeconds(Random.Range(0.3f, 0.7f));
			for (int i = 0; i < AllObjectsInfo.SCRT_EnemyList.Length; i++)
			{
				if (!(AllObjectsInfo.enemy[i].hp > 0f))
				{
					continue;
				}
				touchDownPos = Player.instance.tf.position;
				if (!Calc.IsVisibleCharacterOnScreen(AllObjectsInfo.enemy[i].pos))
				{
					continue;
				}
				if (Random.Range(0, 5) < 3)
				{
					releasePos = AllObjectsInfo.enemy[i].pos;
				}
				else
				{
					releasePos = new Vector2(Random.Range(-20f, 20f), Random.Range(0f, 8f));
					releasePos += new Vector2(Player.instance.tf.position.x, Player.instance.tf.position.y);
				}
				getEnemyNumberOnTouched = Calc.FindEnemiesForMeleeAttack(releasePos);
				if (getEnemyNumberOnTouched == -1)
				{
					Player.instance.kindOfMotion = 1;
					continue;
				}
				if (!Player.instance.attackingNow)
				{
					currentEnemyNumber = getEnemyNumberOnTouched;
					Player.instance.kindOfMotion = 2;
				}
				break;
			}
		}
		yield return new WaitForSeconds(1f);
		GM.instance.Init();
	}
}
